<%@ page import="dao.TeacherD" %>
<%@ page import="dao.StudentD" %>
<%@ page import="vo.Student" %>
<%@ page import="vo.Teacher" %>
<%@ page import="java.util.Properties" %>
<%@ page import="javax.mail.Session" %>
<%@ page import="javax.mail.internet.MimeMessage" %>
<%@ page import="javax.mail.internet.InternetAddress" %>
<%@ page import="javax.mail.Message" %>
<%@ page import="java.util.Date" %>
<%@ page import="javax.mail.Transport" %>

<%--发送验证码以及验证码输入页面, 若用户没有设置安全邮箱则给出提示信息, 验证码输入正确跳到重置密码页面--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>验证码</title>
    <link rel="stylesheet" href="resources/css/bootstrap.min.css">
    <link href="resources/css/forget.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<h1 style="margin-top: 120px; color: white;font-family: cursive;font-size: 50px " class="text-center">欢迎来到学校教务系统</h1>
<%
    String id = request.getParameter("user");
    TeacherD teacherD = new TeacherD();
    StudentD studentD = new StudentD();
    Teacher teacher = null;
    Student student = null;
    int x = (int) (100000 + Math.random() * (999999 - 100000 + 1));
    String content = Integer.toString(x);
    try {
        teacher = teacherD.findWithId(id);
        student = studentD.findWithId(id);
    } catch (Exception e) {
        out.print(e);
    }

    if (teacher != null) {
        if(teacher.getEmail() == null){
%>
            <script>
                alert("该教师未设置安全邮箱!");
                window.location.href = 'login.jsp';
            </script>
<%
        }else {
            String toMail = teacher.getEmail();
            String title = "验证码";

            try {
                // 1.创建session
                // 创建参数配置, 用于连接邮件服务器的参数配置
                Properties prop = new Properties();
                // 需要请求认证
                prop.put("mail.smtp.auth", true);
                // 根据配置创建会话对象, 用于和邮件服务器交互
                Session s = Session.getInstance(prop);

                // 2.创建MimeMessage
                // 创建一封邮件
                MimeMessage message = new MimeMessage(s);
                // Form：发件人
                message.setFrom(new InternetAddress("1516795316@qq.com"));
                // 设置收件人 To:收件人 CC:抄送人 BCC:密送
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(toMail));
                // Subject：邮件主题
                message.setSubject(title);
                // context：邮件正文 text/plain：纯文本格式
                message.setContent(content, "text/plain;charset=utf-8");
                // 设置发件时间
                message.setSentDate(new Date());
                // 保存设置
                message.saveChanges();

                // 3.发送Transport
                // 根据 Session 获取邮件传输对象
                Transport transport = s.getTransport("smtp");
                // 建立与SMTP服务器的连接，注意填入的不是qq密码，而是用户的授权码
                transport.connect("smtp.qq.com", "1516795316@qq.com", "mexxgxaepgvsihci");
                // 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
                transport.sendMessage(message, message.getAllRecipients());
                transport.close(); // 关闭连接
                session.setAttribute("reset", content); //将验证码信息存入session
%>
                <script>
                alert("发送成功，请到邮箱'<%=toMail%>'查收验证码");
                </script>
                <div class="main">
                    <form role="form" action="teacher/resetPassword.jsp" method="post" onsubmit="return checkCode(this)">
                        <div class="form-group" align="center">
                            <input class="form-control" type="text" name="reset" placeholder="验证码"><br>
                            <input type="hidden" name="id" value="<%=id%>">
                            <input type="hidden" name="email" value="<%=toMail%>">
                            <input type="submit" class="btn btn-success" value="下一步">
                            <input type="button" class="btn btn-info" value="取消" style="margin-left: 20px" onclick="window.location.href='login.jsp'">
                        </div>
                    </form>
                </div>
<%
            } catch (Exception e) {
            out.print(e);
            }
        }

    } else if (student != null) {
        if (student.getEmail() == null) {
%>
            <script>
                alert("该学生未设置安全邮箱!");
                window.location.href = 'login.jsp';
            </script>
<%
        } else {
            String toMail = teacher.getEmail(); //获取收件人邮箱
            String title = "验证码";

            try {
                // 1.创建session
                // 创建参数配置, 用于连接邮件服务器的参数配置
                Properties prop = new Properties();
                // 需要请求认证
                prop.put("mail.smtp.auth", true);
                // 根据配置创建会话对象, 用于和邮件服务器交互
                Session s = Session.getInstance(prop);

                // 2.创建MimeMessage
                // 创建一封邮件
                MimeMessage message = new MimeMessage(s);
                // Form：发件人
                message.setFrom(new InternetAddress("1516795316@qq.com"));
                // To:收件人
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(toMail));
                // Subject：邮件主题
                message.setSubject(title);
                // context：邮件正文 text/plain：纯文本格式
                message.setContent(content, "text/plain;charset=utf-8");
                // 设置发件时间
                message.setSentDate(new Date());
                // 保存设置
                message.saveChanges();

                // 3.发送Transport
                // 根据 Session 获取邮件传输对象
                Transport transport = s.getTransport("smtp");
                // 注意填入的不是qq密码，而是用户的授权码
                transport.connect("smtp.qq.com", "1516795316@qq.com", "qlwlubykxqdvhcbf");
                // 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
                transport.sendMessage(message, message.getAllRecipients());
                transport.close(); // 关闭连接
                session.setAttribute("reset", content); //将验证码信息存入session
%>
                <script>
                    alert("发送成功，请到邮箱‘<%=toMail%>’查收验证码");
                </script>
                <div class="main">
                    <form role="form" action="student/resetPassword.jsp" method="post" onsubmit="return checkCode(this)">
                        <div class="form-group" align="center">
                            <input class="form-control" type="text" name="reset" placeholder="验证码"><br>
                            <input type="hidden" name="id" value="<%=id%>">
                            <input type="hidden" name="email" value="<%=toMail%>">
                            <input type="submit" class="btn btn-success" value="下一步">
                            <input type="button" class="btn btn-info" value="取消" style="margin-left: 20px" onclick="window.location.href='login.jsp'">
                        </div>
                    </form>
                </div>
<%
            } catch (Exception e) {
                out.print(e);
            }
        }
    }else {
%>
        <script>
            alert("该用户不存在!");
            window.location.href = 'forget.jsp';
        </script>
<%
    }
%>
<script>
    function checkCode(form){
        if(form.reset.value === "<%=content%>") {
            return true;
        }
        alert('验证码错误！');
        return false;
    }
</script>
<script src="resources/js/jquery-3.2.1.min.js"></script>
<script src="resources/js/popper.min.js"></script>
<script src="resources/js/bootstrap.min.js"></script>
</body>
</html>
